<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/8/29
 * Time: 21:42
 */

namespace app\admin\controller;
use app\home\model\CourseEvaluate;
/**
 * Class Evaluate
 * @package app\admin\controller
 * 评级管理
 */
use think\Db;
use app\home\model\Pic;
class Evaluate extends Admin
{
    /**
     * 老师列表
     */
    public function index(){
        $name = input('name'); // 老师名字
        $map = [];
        if ($name){
            $map = array(
                'name' => ['like','%'.$name.'%'],
            );
        }
        $list = Db::table('pb_skteacher')
            ->where($map)
            ->order('num')
            ->paginate(10,false,['query' => ['name'=> $name]]);
        $this->assign('list',$list);
        return $this->fetch();
    }
    /**
     * 评价列表
     */
    public function sign(){
        $id = input('id');  // 老师账号
        $score = input('score');  // 筛选分数
        $map = [
            't_id' => $id
        ];
        if ($score){
            $map = [
                't_id' => $id,
                'score' => $score
            ];
        }
        $list = CourseEvaluate::where($map)->field('openid,c_id,id,score,create_time,content')->order('score')->paginate(10);
        $this->assign('list',$list);
        $this->assign('id',$id);
        $this->assign('score',$score);
        return $this->fetch();
    }
    /**
     * @return mixed
     * 预览
     */
    public function preview(){
        $id = input('id');
        $Info = CourseEvaluate::where(['id' => $id])->field('content,images')->find();
        if ($Info['images']){
            $Img = json_decode($Info['images']);
            $path = [];
            foreach ($Img as $item){
                array_push($path,"/DDDeducation/public".Pic::where('id',$item)->value('path'));
            }
            $Info['images'] = $path;
        }
        $this->assign('info',$Info);
        return $this->fetch();
    }
    /**
     * 删除
     */
    public function del(){
        $id = input('id');  // 对应 id
        if (empty($id)){
            return $this->error('系统参数错误');
        }
        $Images = CourseEvaluate::where(['id' => $id])->value('images');
        if ($Images){
            $Img = json_decode($Images);
            foreach ($Img as $item){
                $filename = './'.Pic::where('id',$item)->value('path');
                //删除
                if(file_exists($filename)){
                    unlink($filename);
                    Pic::where('id',$item)->delete();
                }
            }
        }
        $res = CourseEvaluate::where(['id' => $id])->delete();
        if ($res){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
}